/**
 * Eletron.js
 * 封装了与H5 与 electron的通信
 */

export default class Electron {
    static isInElectron() {
        return navigator.userAgent.toLowerCase().indexOf(' electron/') > -1
    }

    static saveLoginData(data) {
        const loginData = {
            ...data,
            createTime: this.getNextMonthTime(),
        }
        this.setStore({ loginData })
    }

    static deleteLoginData() {
        this.deleteStore('loginData')
    }

    static setStore(key, value) {
        if (!this.isInElectron()) {
            return
        }
        window.electron?.ipcRenderer?.setStoreValue(key, value)
    }

    static getStore(key) {
        if (!this.isInElectron()) {
            return
        }
        return window.electron?.ipcRenderer?.getStoreValue(key)
    }

    static deleteStore(key) {
        if (!this.isInElectron()) {
            return
        }
        window.electron?.ipcRenderer?.deleteStore(key)
    }

    // 保存一个月 （示例，根据实际情况来做）
    static getNextMonthTime() {
        const date = new Date()

        const currentMonth = date.getMonth()
        const currentYear = date.getFullYear()

        let nextMonth = currentMonth + 1
        let nextYear = currentYear

        if (nextMonth > 11) {
            nextMonth = 0
            nextYear += 1
        }

        return new Date(nextYear, nextMonth).getTime()
    }
}
